package com.own.sign.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.URLDecoder;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

public class CharacterFilterServlet  extends HttpServlet implements Filter {

    private static final long serialVersionUID = 2443806834885344806L;
    public static final String DEFAULT_CHARSET = "UTF-8";
    private static final Map<String, Method> methods = new HashMap();
    private String encoding;

    public CharacterFilterServlet() {
    }

    public void init(FilterConfig filterConfig) throws ServletException {
        this.encoding = filterConfig.getInitParameter("encoding");
        if (this.encoding == null || this.encoding.length() == 0) {
            this.encoding = "UTF-8";
        }

    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest)request;
        HttpServletResponse res = (HttpServletResponse)response;
        res.addHeader("P3P", "CP=CAO PSA OUR");
        if ("GET".equals(req.getMethod().toUpperCase())) {
            chain.doFilter((new CharacterFilterServlet.RequestWrapper(req)).getRequest(), response);
        } else {
            request.setCharacterEncoding(this.encoding);
            chain.doFilter(request, response);
        }

    }

    static {
        Method[] ms = CharacterFilterServlet.RequestWrapper.class.getDeclaredMethods();
        Method[] var1 = ms;
        int var2 = ms.length;

        for(int var3 = 0; var3 < var2; ++var3) {
            Method m = var1[var3];
            int modifiers = m.getModifiers();
            if (Modifier.isProtected(modifiers) && !Modifier.isStatic(modifiers)) {
                methods.put(m.getName(), m);
            }
        }

    }

    class RequestWrapper implements MethodInterceptor {
        private HttpServletRequestWrapper req;
        private HttpServletRequest request;
        private Map<String, Object> params = new HashMap();

        public RequestWrapper(HttpServletRequest request) {
            this.request = request;
            this.req = new HttpServletRequestWrapper(request);
            String queryString = request.getQueryString();
            if (queryString != null && queryString.indexOf("=") > -1) {
                this.parseQueryString(queryString);
            }

        }

        public HttpServletRequest getRequest() {
            Enhancer e = new Enhancer();
            e.setSuperclass(HttpServletRequestWrapper.class);
            e.setCallback(this);
            e.setClassLoader(Enhancer.class.getClassLoader());
            return (HttpServletRequest)e.create(new Class[]{HttpServletRequest.class}, new Object[]{this.request});
        }

        private void parseQueryString(String queryString) {
            try {
                String[] str = queryString.split("&");
                String[] var3 = str;
                int var4 = str.length;

                for(int var5 = 0; var5 < var4; ++var5) {
                    String s = var3[var5];
                    int k = s.indexOf(61);
                    if (k != -1) {
                        this.append(s.substring(0, k), URLDecoder.decode(s.substring(k + 1), CharacterFilterServlet.this.encoding));
                    }
                }

            } catch (UnsupportedEncodingException var8) {
                throw new UnsupportedOperationException(var8);
            }
        }

        private void append(String name, String value) {
            Object obj = this.params.get(name);
            if (obj != null && obj.getClass().isArray()) {
                String[] s = (String[])((String[])obj);
                String[] str = new String[s.length + 1];
                System.arraycopy(s, 0, str, 0, s.length);
                str[s.length] = value;
                this.params.put(name, str);
            } else {
                this.params.put(name, value);
            }

        }

        public Object intercept(Object object, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
            String methodName = method.getName();
            Method m = (Method)CharacterFilterServlet.methods.get(methodName);
            return m != null ? m.invoke(this, args) : method.invoke(this.req, args);
        }

        protected RequestDispatcher getRequestDispatcher(String path) {
            RequestDispatcher rd = this.request.getRequestDispatcher(path);
            int k = path.indexOf(63);
            if (k != -1) {
                String queryString = path.substring(k + 1);
                this.parseQueryString(queryString);
            }

            return rd;
        }

        protected String getParameter(String name) {
            String[] value = this.getParameterValues(name);
            if (value == null) {
                return null;
            } else if (value.length == 0) {
                return value[0];
            } else {
                StringBuilder sb = new StringBuilder();
                String[] var4 = value;
                int var5 = value.length;

                for(int var6 = 0; var6 < var5; ++var6) {
                    String v = var4[var6];
                    sb.append(v);
                    sb.append(",");
                }

                return sb.substring(0, sb.length() - 1);
            }
        }

        protected Enumeration getParameterNames() {
            return new Enumeration() {
                private Iterator it;

                {
                    this.it = RequestWrapper.this.params.keySet().iterator();
                }

                public boolean hasMoreElements() {
                    return this.it.hasNext();
                }

                public Object nextElement() {
                    return this.it.next();
                }
            };
        }

        protected String[] getParameterValues(String name) {
            Object value = this.params.get(name);
            if (value != null) {
                return value.getClass().isArray() ? (String[])((String[])value) : new String[]{(String)value};
            } else {
                return null;
            }
        }
    }

}
